package com.iisigroup.log.statistics;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

public class Testclass {

    /**
     * @param args
     */
    public static void main(String[] args) {
	Testclass sample =new Testclass();
	sample.testMethod2();
    }
    protected static List<String>  extractData(final String expr ,final String src){
	 final List<String> init=new ArrayList<String>();
	
	 final Pattern pattern = Pattern.compile(expr);
	 final Matcher matcher = pattern.matcher(src);
	 while(matcher.find()){	    
	     String extraData = matcher.group();
	     init.add(extraData);
	 }
	 return init;
   }
    public void testMethod2(){
	
	String pattern1="-*\\s*Total\\s*.*\\[AuthorityFilter\\]\\s*(.*?)milliseconds.";
	String sample1 ="-- Total               [AuthorityFilter]  1775.742 milliseconds.";
	System.out.println(sample1.matches(pattern1));
	final	Pattern patternClass1 = Pattern.compile(pattern1);
	final Matcher matcher = patternClass1.matcher(sample1);
	if (matcher.find()) {
	    int groupCount = matcher.groupCount();
	    System.out.println("groupCountA: " + groupCount);
	    for (int i = 0; i <= groupCount; ++i) {
		 System.out.println("group: "+i+"  "+matcher.group(i));
	    }
	}
	Pattern p = Pattern.compile("Print\\s(.*?)\\sdbdrbdr");
	Matcher m = p.matcher(" drbdrbrd  Print this dbdrbdr");
	if(m.find()){
	    int groupCount = m.groupCount();
	    System.out.println("groupCountB "+groupCount);
	    for(int i=0 ;i <=groupCount ;++i ){
		 System.out.println("group: "+i+"  "+m.group(i));
	    }
//	    System.out.println(m.group(1));
	}
	;
	
	
	String mydata = "some string with 'the data i want' inside";
	Pattern pattern2 = Pattern.compile("'(.*?)'");
	Matcher matcher2 = pattern2.matcher(mydata);
	if (matcher2.find())
	{
	    int groupCount = matcher2.groupCount();
	    System.out.println("groupCountC: "+groupCount);
	    System.out.println(matcher2.group());
	    System.out.println(matcher2.group(1));
	}
    }
    public void testMethod1(){
//	String pattern="[%d{MM/dd-HH:mm:ss.SSS}][%-5level] %msg\(%c.java:%L\)%n";
	String pattern="[%d{MM/dd-HH:mm:ss.SSS}][%-5level] %msg\\(%c.java:%L\\)";
//	String pattern01="^(1[0-2]|0[1-9])\/(3[01]|[12][0-9]|0?[1-9])-(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9]).\d{3}";
	String patternDateTime="(1[0-2]|0[1-9])\\/(3[01]|[12][0-9]|0?[1-9])\\-(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9]).\\d{3}";
	String patternDateTime1="(1[0-2]|0[1-9])\\/(3[01]|[12][0-9]|0?[1-9])\\-(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9]).\\d{3}\\|\\w*:\\d*\\W*\\|\\w*\\|\\w{9}\\|\\w*\\W*\\|\\w*\\|(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\W*\\|\\w{1}\\|.*\\)$";
	String patternDateTime2="(1[0-2]|0[1-9])\\/(3[01]|[12][0-9]|0?[1-9])\\-(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9]).\\d{3}\\|\\w*:\\d*\\W*\\|\\w*\\|.*(\n.*)*\\d?\\)";
	String patternDateTime3="(1[0-2]|0[1-9])\\/(3[01]|[12][0-9]|0?[1-9])\\-(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9]).\\d{3}\\|\\w*:\\d*\\W*\\|\\w*\\|\\w{9}\\|\\w*\\W*\\|\\w*\\|(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\W*\\|\\w{1}\\|.*\\)$";
	String patternDateTime4="(1[0-2]|0[1-9])\\/(3[01]|[12][0-9]|0?[1-9])\\-(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9]).*((\n.*)*)\\d?\\)";
	String patternDateTime5="^(1[0-2]|0[1-9])\\/(3[01]|[12][0-9]|0?[1-9])\\-(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9]).*\\d?\\)$";
	String patternDateTime6="^(1[0-2]|0[1-9])\\/(3[01]|[12][0-9]|0?[1-9])\\-(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9]).*";
	String patternDateTime7=".*\\d?\\)$";
	
	String sample ="05/09-17:12:45.063|WB:109 |TK|RU0100013|UNKNOW   |TKULRL1405091712450613|195.15.1.30    |D|putExecutant : TXULRL1405091712450593(tw.gov.moi.ae.common.ThreadLocalUtil:198)";
//	System.lineSeparator(); 
	File srcFile =new File("/home/weblogic/Desktop/test");
	try {
//	    String sample02 = FileUtils.readFileToString(srcFile);
//	    final List<String> setData = extractData(patternDateTime, sample02);
//	    System.out.println(setData.size());
//	    for (String line : setData) {
//		System.out.println("-----------------------------------------------------");
//		System.out.println(line);
//	    }
	   List<String> lines = FileUtils.readLines(srcFile);
	   for(String line : lines){
	       if(line.matches(patternDateTime5)){
		   System.out.println(line);
	       }else if(line.matches(patternDateTime6)){
		   System.out.println(line);
	       }else if(line.matches(patternDateTime7)){
		   System.out.println(line);
	       }
	   }
	} catch (IOException e) {
	    e.printStackTrace();
	}
	
    }
    
}
